<script type="text/javascript">
	$(document).ready(function() {
		/*单击删除已选联系人*/
		$(document).on("click", ".inputbox .address_list a.del", function() {
			$(this).parent().parent().remove();
		});

		/* 查找联系人input 功能*/
		$(document).on("click", ".inputbox .search li",function() {
			name = $(this).text().replace(/<.*>/, '');
			email = $(this).find("a").attr("title");
			html = conv_inputbox_item(email, name, email, email);

			inputbox = $(this).parents(".inputbox");
			inputbox.find("span.address_list").append(html);
			inputbox.find("input.letter").val("");
			inputbox.find(".search ul").html("");
			inputbox.find(".search ul").hide();
			inputbox.find(".search").hide();
		});
		
		/* 查找联系人input 功能*/
		$(".inputbox .letter").keyup(function(e) {
			switch(e.keyCode) {
				case 40:
					var $curr = $(this).parents(".inputbox").find(".search li.active").next();
					if ($curr.html() != null) {
						$(this).parents(".inputbox").find(".search li").removeClass("active");
						$curr.addClass("active");
					}
					break;
				case 38:
					var $curr = $(this).parents(".inputbox").find(".search li.active").prev();
					if ($curr.html() != null) {
						$(this).parents(".inputbox").find(".search li").removeClass("active");
						$curr.addClass("active");
					}
					break;
				case 13:
					if ($(this).parents(".inputbox").find(".search ul").html() != "") {
						name = $(".search li.active").text().replace(/<.*>/, '');
						email = $(".search li.active a").attr("title");
						html = conv_inputbox_item(email, name, email, email);
						$(this).parents(".inputbox").find("span.address_list").append(html);
						$(this).parents(".inputbox").find(".search ul").html("");
						$(this).val("");
						$(this).parents(".inputbox").find(".search ul").hide();
					} else {
						email = $(this).val();
						if (validate(email, 'email')) {
							name = email;
							html = conv_inputbox_item(email, name, email, email);
							$(this).parents(".inputbox").find("span.address_list").append(html);
							$(this).val("");
						} else {
							ui_error("邮件格式错误");
							return false;
						}
					}
					break;
				default:
					var search = $(this).parents(".inputbox").find("div.search ul");
					if ($(this).val().length > 1) {
						$.getJSON("{:U('popup/json','type=all')}", {
							key : $(this).val()
						}, function(json) {
							if (json != "") {
								if (json.length > 0) {
									search.html("");
									$.each(json, function(i) {
										search.append('<li><a title="' + json[i].email + '">' + json[i].name + '&lt;' + json[i].email + '&gt;</a></li>');
									});
									search.children("li:first").addClass("active");
									search.show();
								}
							} else {
								search.html("");
								search.hide();
							}
						});
					} else {
						search.hide();
					}
			}
		});
	});

	function send(flag){
		window.onbeforeunload=null;
		$("#to").val("");
		$("#recever .address_list span").each(function(){
			$("#to").val($("#to").val() + $(this).find("b").text() + '|' + $(this).attr("data") +'|' + $(this).attr("id") + ";");
		});

		$("#cc").val("");
		$("#carbon_copy .address_list span").each(function() {
			$("#cc").val($("#cc").val() + $(this).find("b").text() + '|' + $(this).attr("data") +'|' + $(this).attr("id") + ";");
		});
	
		$("#bcc").val("");
		$("#blind_carbon_copy .address_list span").each(function() {
			$("#bcc").val($("#bcc").val() + $(this).find("b").text() + '|' + $(this).attr("data") +'|' + $(this).attr("id") + ";");
		});

		if ($("#to").val().indexOf("@") < 1) {
			ui_error("请选择收件人");
			$("#recever .letter").focus();
			return false;
		}
		if (check_form("form_send")) {
			if (flag) {
				sendForm("form_send", "{:U('send')}");
			} else {
				sendForm("form_send", "{:U('save_darft')}");
			}
		}
	}

	function toggle_bcc() {
		if ($("#bcc_wrap").attr("class").indexOf("display-none") < 0) {
			$("#bcc_wrap").addClass("display-none");
			$("#toggle_bcc_icon").addClass("fa-chevron-down");
			$("#toggle_bcc_icon").removeClass("fa-chevron-up");
		} else {
			$("#bcc_wrap").removeClass("display-none");
			$("#toggle_bcc_icon").addClass("fa-chevron-up");
			$("#toggle_bcc_icon").removeClass("fa-chevron-down");
		}
	}

	function popup_contact() {
		winopen("{:U('popup/contact')}", 730, 570);
	}
</script>